package ltd.newbee.mall.controller.admin;

import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.entity.Hall;
import ltd.newbee.mall.entity.NewBeeMallGoods;
import ltd.newbee.mall.entity.Schedule;
import ltd.newbee.mall.service.ScheduleService;
import ltd.newbee.mall.util.PageQueryUtil;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("/Schedule")
public class ScheduleController {

    @Resource
    ScheduleService scheduleService;

    @GetMapping("/Schedule")
    public String goodsPage(HttpServletRequest request) {
        request.setAttribute("path", "newbee_mall_goods");
        return "admin/film_hall";
    }
    //反显数据
    @RequestMapping(value="/selectAll", method = RequestMethod.GET)
    @ResponseBody
    public Result selectAll(@RequestParam Map<String, Object> params){
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(scheduleService.selectAll(pageUtil));
    }


    /**
     * 添加
     */
    @RequestMapping(value="/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody Schedule schedule) {
        if (StringUtils.isEmpty(schedule.getFilmId())
                || StringUtils.isEmpty(schedule.getHallId())
                || StringUtils.isEmpty(schedule.getShowDate())
                || StringUtils.isEmpty(schedule.getShowTime())) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result=scheduleService.saveSchedule(schedule);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }
    /**
     * 修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestBody Schedule schedule) {
        if (StringUtils.isEmpty(schedule.getFilmId())
                || StringUtils.isEmpty(schedule.getScheduleId())
                || StringUtils.isEmpty(schedule.getHallId())
                || StringUtils.isEmpty(schedule.getShowDate())
                || StringUtils.isEmpty(schedule.getShowTime()))  {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result=scheduleService.UpdateSchedule(schedule);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 分类删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (scheduleService.deleteSchedule(ids)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }

}
